package com.info.game;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.component.game.Component;
import com.component.game.PhysicsComponent;
import com.engine.game.GameObject;
import com.engine.game.MessageInfo;


public class GravityRobotPhysicsComponent implements PhysicsComponent{

	public static int numFootContactsLow = 0;
	public static  int numFootContactsUp = 0;
	
	
	private boolean crossing = false;
	private String crossObject = "NOTHING";
	
	private boolean inverse = false;
	
	public static boolean stop = true;
	
	private Body body;
	
	
	@Override
	public void bodyCreation(GameObject object) {
		
		BodyDef robot = new BodyDef();
		robot.type = BodyType.DynamicBody;
		robot.position.set(20, 15);
		robot.fixedRotation = true;
		robot.allowSleep = false;
		
		this.body = object.getPhysicsWorld().createBody(robot);
		
		
		PolygonShape roboShape = new PolygonShape();
		roboShape.setAsBox(SizeOfThings.TimeRobotWidth/2, SizeOfThings.TimeRobotHeight/2);
		
		FixtureDef zoneFix = new FixtureDef();
		zoneFix.shape = roboShape;
		zoneFix.friction = 0.0f;
		this.body.createFixture(zoneFix).setUserData(object.name);
		
		
		FixtureDef footSensorLow = new FixtureDef();
		
		PolygonShape footShapeLow = new PolygonShape();
		footShapeLow.setAsBox(0.3f, 0.3f, new Vector2(0, -3), 0);
		
		footSensorLow.isSensor = true;
		footSensorLow.shape = footShapeLow;
		
		
		this.body.createFixture(footSensorLow).setUserData("FOOT_SENSOR_LOW");;
		
		
		
		
		
        FixtureDef footSensorUp = new FixtureDef();
		
		PolygonShape footShapeUp = new PolygonShape();
		footShapeUp.setAsBox(0.3f, 0.3f, new Vector2(0, 3), 0);
		
		footSensorUp.isSensor = true;
		footSensorUp.shape = footShapeUp;
		
		
		this.body.createFixture(footSensorUp).setUserData("FOOT_SENSOR_UP");
		
		

		
		
		
		this.body.setUserData(object);
		
	}
	
	
	@Override
	public void dispose() {
		this.body.getWorld().destroyBody(this.body);
		
	}

	@Override
	public void update(GameObject object) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		
		if(inverse){
			
			if(GravityRobotPhysicsComponent.numFootContactsUp >= 2 && GravityRobotPhysicsComponent.stop){
				this.body.setLinearVelocity(0, 0);
				GravityRobotPhysicsComponent.stop = false;
			}
		}
		else{
			if(GravityRobotPhysicsComponent.numFootContactsLow >= 1 && GravityRobotPhysicsComponent.stop){
				this.body.setLinearVelocity(0, 0);
				GravityRobotPhysicsComponent.stop = false;
			}
		}
		
		
		for(MessageInfo message : object.getPhysicsMessages()){
			
			if(message.message.contains("jump_up")){
				
				
				
				
				if((GravityRobotPhysicsComponent.numFootContactsLow >= 1) && (!this.inverse)){
					float impulse = this.body.getMass() * 10;
					this.body.applyLinearImpulse(new Vector2(0,impulse), this.body.getWorldCenter(), true);
				}
				
				
			}
			else if(message.message.contains("walk_left")){
				
				if(inverse){
					if(GravityRobotPhysicsComponent.numFootContactsUp >= 2){
						this.body.setLinearVelocity(-6, 0);
					}
				}
				else{
					if(GravityRobotPhysicsComponent.numFootContactsLow >= 1){
						this.body.setLinearVelocity(-6, 0);
					}
				}
				
				
			}
			else if(message.message.contains("walk_right")){
				
				
				
				if(inverse){
					
					if(GravityRobotPhysicsComponent.numFootContactsUp >= 2){
						this.body.setLinearVelocity(6, 0);
					}
				}
				else{
					if(GravityRobotPhysicsComponent.numFootContactsLow >= 1){
						this.body.setLinearVelocity(6, 0);
					}
				}
				
			}
			else if(message.message.contains("jump_down")){
				
				if((GravityRobotPhysicsComponent.numFootContactsUp > 1) && (this.inverse)){
					float impulse = this.body.getMass() * (-10);
					this.body.applyLinearImpulse(new Vector2(0,impulse), this.body.getWorldCenter(), true);
				}
				
			}
			else if(message.message.contains("inverse")){
				
				
				
				this.inverse = !this.inverse;
				
				if(this.inverse){
					
					object.generate("create;GRAVITY_ZONE", "CONFIGURATION", Component.NO);
					
				}
				else
				{
					object.generate("die;GRAVITY_ZONE", "CONFIGURATION", Component.NO);
					
				}
				
			}
            else if(message.message.contains("set_gravity_scale")){
				
            	String[] parts = message.message.split(";");
            	
            	
            	
            	this.body.setGravityScale(Integer.parseInt(parts[1]));
				
			}
            else if(message.message.contains("open")){
				
            	if(this.crossing){
            		
            		object.generate("open", this.crossObject, Component.AI);
            		
            	}
				
			}
            else if(message.message.contains("set_standard_gravity")){
            	
            	
            	
            	this.body.setGravityScale(1);
            	
            }
			
			
		}
	
		
		
		
	}

	@Override
	public void collisionWith(GameObject me, String enemy) {
		
	
		
		if((enemy.contains("BUTTON")) || (enemy.contains("DOOR"))){
			this.crossing = true;
			this.crossObject = enemy;
		}
		
		
	}
	
	@Override
	public void endCollisionWith(GameObject me, String enemy) {
		
		if(enemy != null){
			
			if((enemy.contains("BUTTON")) || (enemy.contains("DOOR"))){
				this.crossing = false;
				this.crossObject = "NOTHING";
			}
		
		
		}
	}

	@Override
	public float getCenterBodyX() {
		
		return this.body.getPosition().x;
	}

	@Override
	public float getCenterBodyY() {
		
		return this.body.getPosition().y;
	}

	@Override
	public float getBodyAngle() {
		
		return this.body.getAngle() * MathUtils.radiansToDegrees;
	}



}
